<?php

$mysqlconnect = mysql_connect ('localhost', 'root', 'alex');
if (!$mysqlconnect) {
	exit ('Database server connection failed!');
}

$mysqlselect = mysql_select_db ('explay3_doc', $mysqlconnect);
if (!$mysqlselect) {
	exit ('Database not found!');
}

$mysql_version = mysql_get_server_info();

if (version_compare($mysql_version, '4.1', '>=')) {
	mysql_query("/*!40101 SET NAMES 'utf8' */");
}

if (isset ($_GET['classes'])) {
	$classes = array ();
	
	$getClasses = mysql_query ("SELECT * FROM classes ORDER BY name");
		
	while ($class = mysql_fetch_assoc ($getClasses)) {
		$classes[] = $class;
		
		$className = $class['name'];
		$classDesc = $class['description'];
		
		$html = <<<HTML
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="../style.css" type="text/css" media="all" />
<body>
<h1>{$className}</h1>
<div class="class_description">{$classDesc}</div>
<h2>Методы</h2>
HTML;

		/**
		 * ------------------------- ЗАГРУЗКА МЕТОДОВ И ИХ ПАРАМЕТРОВ
		 */

		$getMethods = mysql_query ("SELECT * FROM methods WHERE class = '{$class[name]}' ORDER BY name");
		
		if (mysql_num_rows ($getMethods) == 0) {
			$html .= <<<HTML
<div>У этого класса нет методов или они не документированы.</div>
HTML;
		}
		
		$methods = array ();
		
		while ($method = mysql_fetch_assoc ($getMethods)) {
			$id = $method['id'];
			$name = $method['name'];
			
			$parametrs = array ();
			$samples = array ();
			
			$getParametrs = mysql_query ("SELECT * FROM methods_parametrs WHERE method_id = {$id} ORDER BY position");
			
			while ($parametr = mysql_fetch_assoc ($getParametrs)) {
				$parametrs[] = $parametr;
			}
			
			$getSamples = mysql_query ("SELECT * FROM methods_samples WHERE method_id = {$id}");
			
			while ($sample = mysql_fetch_assoc ($getSamples)) {
				$samples[] = $sample['content'];
			}
			
			$methods[] = array (
				'method' => $method,
				'parametrs' => $parametrs,
				'samples' => $samples
			);
		}

		/**
		 * ------------------------- ОГЛАВЛЕНИЕ
		 */
		
		$html .= <<<HTML
<ul>
HTML;
		foreach ($methods as $method) {
			$name = $method['method']['name'];
			
			$html .= <<<HTML
<li><a href="#{$name}">{$name}</a></li>
HTML;
		}
		$html .= <<<HTML
</ul>
HTML;

		/**
		 * ------------------------- МЕТОДЫ
		 */
		
		foreach ($methods as $method) {
			$html .= get_method_html ($method['method']['name'], $method['parametrs'], $method['method']['return'], $method['method']['return_description'], $method['method']['description'], $method['samples'], $method['method']['static']);
		}
		
		/**
		 * ------------------------- ПИМЕРЫ
		 */
		
		$getSamples = mysql_query ("SELECT * FROM classes_samples WHERE class = '{$className}'");
		
		if (mysql_num_rows ($getSamples) != 0) {
			$html .= <<<HTML
<h2>Примеры</h2>
HTML;
		}
		
		$n = 0;
		while ($sample = mysql_fetch_assoc ($getSamples)) {
			$n++;
			$sample = highlight_string ($sample['content'], true);
			$html .= <<<HTML
<div class="sample">
<p>Пример №{$n}</p>
<div class="sample_content">{$sample}</div>
</div>
HTML;
		}
		
		$html .= <<<HTML
</body>
</html>
HTML;

		/**
		 * ------------------------- СОЗДАНИЕ ФАЙЛА
		 */

		$file = fopen ('./classes/' . $class['name'] . '.html', 'w+');
		fwrite ($file, $html);
		fclose ($file);
	}
	
	
	
	$n = 0;
	$listHtml = <<<HTML
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="../style.css" type="text/css" media="all" />
<body>
<h1>2. Описание классов ядра</h1>
<ul>
HTML;
	
	foreach ($classes as $class) {
		$n++;
		//print htmlspecialchars ("<li>2.{$n}. <a href=\"classes/{$class[name]}.html\" target=\"content_frame\">{$class[name]}</a>") . "<br>";
		$listHtml .= "<li>2.{$n}. <a href=\"{$class[name]}.html\">{$class[name]}</a></li>\r\n";
	}
	
	$listHtml .= <<<HTML
</ul>
</body>
</html>
HTML;

	$file = fopen ('./classes/index.html', 'w+');
	fwrite ($file, $listHtml);
	fclose ($file);
	
	print 'ok';
}











if (isset ($_GET['functions'])) {
	
	$functions = array ();

	$html = <<<HTML
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="style.css" type="text/css" media="all" />
<body>
<h1>Функции</h1>
HTML;

	$getFunctions = mysql_query ("SELECT * FROM functions ORDER BY name");
	
	while ($function = mysql_fetch_assoc ($getFunctions)) {
		$parametrs = array ();
		$samples = array ();
		
		$getParametrs = mysql_query ("SELECT * FROM func_parametrs WHERE function = '{$function[name]}'");
		
		while ($parametr = mysql_fetch_assoc ($getParametrs)) {
			$parametrs[] = $parametr;
		}
		
		$getSamples = mysql_query ("SELECT * FROM func_samples WHERE function = '{$function[name]}'");
			
		while ($sample = mysql_fetch_assoc ($getSamples)) {
			$samples[] = $sample['content'];
		}
		
		$functions[] = array (
			'function' => $function,
			'parametrs' => $parametrs,
			'samples' => $samples
		);
	}
	
	/**
	 * ------------------------- ОГЛАВЛЕНИЕ ФУНКЦИЙ
	 */
		
	$html .= <<<HTML
<ul>
HTML;
	foreach ($functions as $function) {
		$name = $function['function']['name'];
		
		$html .= <<<HTML
<li><a href="#{$name}">{$name}</a></li>
HTML;
	}
	$html .= <<<HTML
</ul>
HTML;

	/**
	 * ------------------------- ФУНКЦИИ
	 */
		
	foreach ($functions as $function) {
		$html .= get_method_html ($function['function']['name'], $function['parametrs'], $function['function']['return'], $function['function']['return_description'], $function['function']['description'], $function['samples'], 0);
	}
	
	$html .= <<<HTML
</body>
</html>
HTML;

	$file = fopen ('./functions.html', 'w+');
	fwrite ($file, $html);
	fclose ($file);
	
	print 'ok';
}

















function get_method_html ($name, array $vars, $return, $returnDesc, $desc, $samples, $static) {
	$staticHTML = $static == 1 ? '<span class="static">static</span> ' : '';
	
	$html = <<<HTML
<a name="{$name}"></a>
<h3>{$name}</h3>
<div class="block description">
<h4>Описание</h4>
<div class="text method_description">
<div class="prototype">{$staticHTML}<span class="return">{$return}</span> <span class="name">{$name}</span> (
HTML;

	$varsPrototype = array ();
	$varsDesc = array ();
	
	foreach ($vars as $var) {
		$default = '';
		if ($var['default'] != '') {
			if (is_numeric ($var['default']) || $var['default'] == 'false' || $var['default'] == 'true') {
				$default = ' = ' . $var['default']; 
			} else {
				$default = ' = \'' . $var['default'] . '\''; 
			}
		}
		
		$varsPrototype[] = '<span class="type">' . $var['type'] . '</span> <span class="varname">$' . $var['name'] . '</span>' . $default;
		
		if ($var['description'] != '') {
			$varsDesc[] = array ($var['name'], $var['description']);
		}
	}
	
	$html .= implode (', ', $varsPrototype);
	
	$html .= ')</div>';
	$html .= <<<HTML
<p>{$desc}</p>
HTML;

	if ($returnDesc) {
		$html .= <<<HTML
<p><strong>Результат: </strong>{$returnDesc}</p>	
HTML;
	}
	
	$html .= "\r\n</div>\r\n</div>";
	
	if (count ($varsDesc)) {
		$html .= <<<HTML
<div class="block parametrs">
<h4>Параметры</h4>
<div class="text parametrs">
HTML;
		
		foreach ($varsDesc as $var) {
			$html .= <<<HTML
<dl class="var_description">
	<dt>{$var[0]}</dt>
	<dd>{$var[1]}</dd>
</dl>
HTML;
		}
		
		$html .= "\r\n</div>\r\n</div>";
	}

	if (count ($samples)) {
		$html .= <<<HTML
<div class="block samples">
<h4>Примеры</h4>
HTML;
		
		$n = 0;
		foreach ($samples as $sample) {
			$n++;
			$sample = highlight_string ($sample, true);
			$html .= <<<HTML
<div class="sample">
<p>Пример №{$n}</p>
<div class="sample_content">{$sample}</div>
</div>
HTML;
		}
		
		$html .= "\r\n</div>";
	}
	
	return $html;
}
